function p_put = barrier_put(S0, mu, sigma, K, T, N, M, b_ratio)
price_mat = zeros(N + 1, M);

for i = 1 : M
    price_mat(:, i) = underlyingpath(S0, mu, sigma, T, N);
end

p_barrier = S0 * (1 - b_ratio);
p_final = zeros(1, size(price_mat, 2));
for i = 1 : size(price_mat, 2)
    this = price_mat(:, i);
    loc = this <= p_barrier;
    if (any(loc))
        this(~loc) = p_barrier;
        p_final(i) = mean(this);
    else
        p_final(i) = this(end);
    end
end

p_put = mean(max(K - p_final, 0)) * exp(-mu * T);


end